#ifndef __EVENT_PUBLISHER_H
#define __EVENT_PUBLISHER_H

#include <algorithm>
#include <vector>
#include <string>
#include "rclcpp/rclcpp.hpp"
#include <agv_msgs/msg/agv_event.hpp>

namespace common {

#define AGVEvent_TO_STREAM(stream, event) \
    stream << "AGVEvent:\n"; \
    stream << "  header:\n"; \
    stream << "    stamp: " << event.header.stamp.sec << "." << std::setfill('0') << std::setw(9) << event.header.stamp.nanosec << "\n"; \
    stream << "    frame_id: " << event.header.frame_id << "\n"; \
    stream << "  type: " << static_cast<int>(event.type) << "\n"; \
    stream << "  action: " << static_cast<int>(event.action) << "\n"; \
    stream << "  ackreq: " << static_cast<int>(event.ackreq) << "\n"; \
    stream << "  eventtime: " << std::fixed << std::setprecision(3) << event.eventtime << "s\n"; \
    stream << "  vellimit: " << event.vellimit << "m/s\n"; \
    stream << "  ecode: " << event.ecode << "\n"; \
    stream << "  param1: " << event.param1 << "\n"; \
    stream << "  param2: " << event.param2 << "\n"; \
    stream << "  param3: " << event.param3 << "\n"; \
    stream << "  param4: " << event.param4 << "\n"; \
    stream << "  description_cn: \"" << event.description_cn << "\"\n"; \
    stream << "  description: \"" << event.description << "\""

inline std::ostream& operator<<(std::ostream& os, const agv_msgs::msg::AGVEvent& event) {
  AGVEvent_TO_STREAM(os, event);
  return os;
}

class EventPublisher
{

public: 
	EventPublisher();
	~EventPublisher();

	void init(rclcpp::Node::SharedPtr node);
	void add(agv_msgs::msg::AGVEvent &event, int unique_index, int unique_value);
	void clear(int unique_index, int unique_value);
	void clear();
	void eventtimer_callback();

private:
	rclcpp::Node::SharedPtr  				node_;
	std::vector<agv_msgs::msg::AGVEvent> 	events_;
	std::shared_ptr<rclcpp::Publisher<agv_msgs::msg::AGVEvent>>  events_pub_;
	rclcpp::TimerBase::SharedPtr			event_timer_;
	rclcpp::Time 						    last_publish_time_;
};

}

#endif